package com.example.aibodytest

import android.content.Context
import android.graphics.Bitmap
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.example.aibodytest.ui.theme.AibodytestTheme
import com.aibody.analysis.AgeGenderData
import com.aibody.analysis.AnalysisBody
import com.aibody.analysis.EGenderType
import com.aibody.analysis.PostureAnalysisResultItem

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            AibodytestTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Greeting(
                        context = this,
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}

@Composable
fun Greeting(context: Context, modifier: Modifier = Modifier) {
    val count = remember { mutableStateOf(0) }
    val drawnImages = remember { mutableStateOf<Pair<Bitmap, Bitmap>?>(null) } // 用于保存绘制后的图片


    val ageGender = remember { mutableStateOf(AgeGenderData(
        age = "",
        gender = "",
        genderType = EGenderType.MALE
    )) }

    val bodyList = remember { mutableStateOf(mutableListOf<PostureAnalysisResultItem>()) }

    var img1 = "http://124.223.10.159:8001/public/uploads/20241022/7bb90940-908c-11ef-814e-7f0f11982689.jpg";

    var img2 = "http://124.223.10.159:8001/public/uploads/20241104/ec36d8c0-9a85-11ef-bfb1-7b89283d9fc7.jpg";

    // 使用 remember 来确保线程不会重复启动
    val isProcessing = remember { mutableStateOf(false) }
    if (!isProcessing.value) {
        isProcessing.value = true // 标记为正在处理

        val thread = Thread {
            // 这里是耗时的操作
            val image = ImageDownloader.downloadBitmapFromUrl(img1)
            val image1 = ImageDownloader.downloadBitmapFromUrl(img2)

            if (image != null) {
                val analysisBody = AnalysisBody(context);
                analysisBody.init("xxxxxxxxxxxxxxxxxxxxxx") {
                    if (it) {
                        image1?.let { it1 ->
                            analysisBody.detectBodyPosture(image, it1) { result, images ->
                                bodyList.value = result.result.toMutableList() // 更新体态分析结果
                                println("体态分析结果: ${bodyList.value}")

                                // 这里可以处理绘制过的图片
                                drawnImages.value = images // 保存绘制后的图片
                            }
                        }

                        analysisBody.detectAgeGender(
                            image
                        ) {
                            ageGender.value = it
                        }
                    }
                }

            }
        }


        thread.start();
    }

    Column {

//        Row {
//            AsyncImage(
//                model = img1,
//                contentDescription = null
//            )
//            AsyncImage(
//                model = img2,
//                contentDescription = null
//            )
//        }

//         显示绘制后的图片
        drawnImages.value?.let { (frontImage, sideImage) ->
            Image(
                painter = BitmapPainter(frontImage.asImageBitmap()),
                contentDescription = "正面绘制图",
                modifier = Modifier.size(300.dp)
            )
            Image(
                painter = BitmapPainter(sideImage.asImageBitmap()),
                contentDescription = "侧面绘制图",
                modifier = Modifier.size(300.dp)
            )
        }

        Text(
            text = "年龄 ${ageGender.value.age}"
        )

        Text(
            text = "性别 ${ageGender.value.gender}"
        )

        Button(onClick = { count.value ++ /* Do something when button is clicked */ }) {
            Text("Button")
        }
    }
}


@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    AibodytestTheme {
        Text("14214")
    }
}